package com.platform.pay.point.mq;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.*;
import com.platform.pay.point.util.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Properties;

/**
 * Created by churcy on 2017/5/31.
 */
public class UserPayPointCustomer {
    private static final Logger logger = LoggerFactory.getLogger(UserPayPointCustomer.class);
    public static final String CUSTOMER_ID = "CID_TRADE_USER_POINT_P";
    public static final String ACCESS_KEY = "LTAIqzufBH34q3N4";
    public static final String SECRET_KEY = "0Q62IKwEjoJd5aIlhiPx3hcEA3EWH9";
    public static final String TOPIC = "P_TOPIC_TRADE_USER_POINT";
    public static final String TAG = "TAG_TRADE_USER_POINT_P";
    public static final String SERVER_URL = "http://121.40.76.23:9089/credit_manage/creditInter/addCreditByConsume.do";
    public static final String LOCAL_URL = "http://actioning.ngrok.cc/credit_manage/creditInter/addCreditByConsume.do";
    public static final Properties properties = new Properties();

    static {
        properties.put(PropertyKeyConst.ConsumerId, CUSTOMER_ID);//您在控制台创建的Producer ID
        properties.put(PropertyKeyConst.AccessKey, ACCESS_KEY);// AccessKey 阿里云身份验证，在阿里云服务器管理控制台创建
        properties.put(PropertyKeyConst.SecretKey, SECRET_KEY);// SecretKey 阿里云身份验证，在阿里云服务器管理控制台创建
        properties.setProperty(PropertyKeyConst.SendMsgTimeoutMillis, "3000");//设置发送超时时间，单位毫秒
        properties.put(PropertyKeyConst.ONSAddr,
                "http://onsaddr-internal.aliyun.com:8080/rocketmq/nsaddr4client-internal");//此处以公共云生产环境为例
    }

//    public static void main(String[] args) {
//        Consumer consumer = ONSFactory.createConsumer(properties);
//        consumer.start();
//        logger.info("consumer start");
//        consumer.subscribe(TOPIC, TAG, new MessageListener() {
//            @Override
//            public Action consume(final Message message, final ConsumeContext consumeContext) {
//                try {
//                    String jsonMessage = new String(message.getBody());
//                    logger.info("receive message:" + jsonMessage);
//                    JSONObject jsonObject = JSONObject.parseObject(jsonMessage);
//                    String userId = jsonObject.getString("userId");
//                    String transactionTime = jsonObject.getString("actualOrderTime");
//                    String paymentTime = jsonObject.getString("updateTime");
//                    StringBuffer stringBuffer = new StringBuffer();
//                    stringBuffer.append("userId=" + userId);
//                    stringBuffer.append("&transactionTime=" + URLEncoder.encode(transactionTime, "UTF-8"));
//                    stringBuffer.append("&paymentTime=" + URLEncoder.encode(paymentTime, "UTF-8"));
//                    String response = HttpUtil.httpGet(SERVER_URL, null, stringBuffer.toString());
//                    logger.info("receive response:" + response);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//                return Action.CommitMessage;
//            }
//        });
//    }
}
